<template>
  <!-- 设备管理 -->
  <div id="financeSettleByDiv">
    <vxe-modal v-model="isShowPeople" width="65vw" show-footer :mask="false">
      <template #title>
        <span class="text-bold">单据信息</span>
      </template>
      <template #default>
        <div class="tableBody" style="height: 600px">
          <Splitpanes>
            <Pane size="54">
              <div class="tableBox">
                <vxe-table ref="settleTableRef" :data="settleList" :loading="mLoading" @current-change="settleRowClick" class="mytable-scrollbar supplierTableClazz" border="inner" align="center" height="auto" :row-config="{ isCurrent: true, isHover: true }" :column-config="{ resizable: true }" resizable show-overflow keep-source>
                  <vxe-column field="settleNo" title="单据号" width="100"> </vxe-column>
                  <vxe-column field="companyName" title="单位名称"></vxe-column>
                  <vxe-column field="costs" title="金额" width="80"></vxe-column>
                  <vxe-column field="realCosts" title="实际收费" width="80"></vxe-column>
                  <vxe-column field="brokerageCosts" title="提成金额" width="80"></vxe-column>
                  <vxe-column field="settleDate" title="结算时间" width="150"></vxe-column>
                </vxe-table>
              </div>
            </Pane>
            <Pane size="46">
              <el-tabs type="border-card" v-model="activeName" @tab-click="tabHandleClick">
                <el-tab-pane label="结算明细" name="first" :disabled="IsEmpty(sltSettle.id)">
                  <div class="tableBoBox">
                    <vxe-table ref="reqDetailTableRef" :data="settleDetailList" :loading="dLoading" class="mytable-scrollbar supplierTableClazz" border="inner" align="center" height="auto" :row-config="{ isCurrent: true, isHover: true }" :column-config="{ resizable: true }" resizable show-overflow keep-source>
                      <vxe-column type="seq" width="24" fixed="left"></vxe-column>
                      <vxe-column field="feeCode" title="项目代号" width="100"> </vxe-column>
                      <vxe-column field="feeName" title="项目名称" width="150"> </vxe-column>
                      <vxe-column field="price" title="单价" width="80"></vxe-column>
                      <vxe-column field="amount" title="数量" width="100"></vxe-column>
                      <vxe-column field="costs" title="金额" width="100"></vxe-column>
                    </vxe-table>
                  </div>

                </el-tab-pane>
                <el-tab-pane label="申请信息" name="second" :disabled="IsEmpty(sltSettle)||IsEmpty(sltSettle.id)">
                  <div class="tableBoBox">
                    <vxe-table ref="reqMainTableRef" :data="reqMainList" :loading="rLoading" class="mytable-scrollbar supplierTableClazz" border="inner" align="center" height="auto" :row-config="{ isCurrent: true, isHover: true }" :column-config="{ resizable: true }" resizable show-overflow keep-source>
                      <vxe-column type="seq" width="24" fixed="left"></vxe-column>
                      <vxe-column field="reqNo" title="条码号"></vxe-column>
                      <vxe-column field="patNo" title="病历号"></vxe-column>
                      <vxe-column field="patNa" title="患者姓名"></vxe-column>
                      <vxe-column field="reqPurpose" title="检测项目"></vxe-column>
                    </vxe-table>
                  </div>
                </el-tab-pane>
              </el-tabs>

            </Pane>
          </Splitpanes>
        </div>
      </template>
    </vxe-modal>
  </div>
</template>
<script setup lang="ts">
import { Splitpanes, Pane } from 'splitpanes';
import { ref, reactive, getCurrentInstance } from 'vue';
import Breadcrumb from '@/components/Breadcrumb.vue';
import { IsEmpty, formatDateTime, autoIncrementNumber, sortByField } from '@/utils/common';
let activeName = ref('first');

let { proxy }: any = getCurrentInstance();
let settleList: any = reactive([]);
let settleDetailList: any = reactive([]);
let reqMainList: any = reactive([]);
let mLoading = ref(false); //刷新加载状态
let dLoading = ref(false); //刷新加载状态
let rLoading = ref(false); //刷新加载状态
let sltSalesSettle: any = ref({});
let sltSettle: any = ref({});

let isShowPeople = ref(false);

//暴露属性和方法
defineExpose({
  initPage
});
function initPage(row: any) {
  isShowPeople.value = true;
  activeName.value = 'first';
  sltSalesSettle.value = row;
  findFinanceSettleList();
}

function findFinanceSettleList() {
  settleList.length = 0;
  mLoading.value = true;
  const url = '/lab/financeSalesSettle/findFinanceSettlesBySales';
  proxy.$http
    .get(url, {
      params: {
        salesSettleId: sltSalesSettle.value.salesSettleId,
        companyCode: sltSalesSettle.value.companyCode
      }
    })
    .then((res: any) => {
      settleList.push(...res.data);
      if (settleList.length == 0) {
        sltSettle.value = {};
        settleDetailList.length = 0;
        reqMainList.length = 0;
      } else {
        const kn = settleList.find((k: any) => k.id == sltSettle.value.id);
        if (IsEmpty(kn)) {
          settleRowClick({ row: settleList[0] }); //默认选中第一条数据
        } else {
          settleRowClick({ row: kn }); //默认选中第一条数据
        }
      }
      mLoading.value = false;
    })
    .catch((err: any) => {
      mLoading.value = false;
      proxy.$alert(err, '提示');
    });
}

function settleRowClick({ row }: any) {
  if (IsEmpty(row)) {
    sltSettle.value = {};
    settleDetailList.length = 0;
    reqMainList.length = 0;
  } else {
    (proxy.$refs.settleTableRef as any).setCurrentRow(row);
    sltSettle.value = row;

    if (activeName.value == 'first') {
      findSettleDetailList();
    } else if (activeName.value == 'second') {
      findReqMainBySettleList();
    }
  }
}

function tabHandleClick(tab) {
  if (tab.props.name == 'first') {
    findSettleDetailList();
  } else if (tab.props.name == 'second') {
    findReqMainBySettleList();
  }
}

function findSettleDetailList() {
  settleDetailList.length = 0;
  dLoading.value = true;
  const url = '/lab/financeSettleDetail/findFinanceSettleDetailList';
  proxy.$http
    .get(url, {
      params: {
        //settleNo: sltSettle.settleNo,
        settleId: sltSettle.value.id
      }
    })
    .then((res: any) => {
      settleDetailList.length = 0;
      settleDetailList.push(...res.data);

      dLoading.value = false;
    })
    .catch((err: any) => {
      dLoading.value = false;
      proxy.$alert(err, '提示');
    });
}

function findReqMainBySettleList() {
  reqMainList.length = 0;
  rLoading.value = true;
  const url = '/lab/financeSettle/findReqMainBySettleList';
  proxy.$http
    .get(url, {
      params: {
        settleId: sltSettle.value.id
      }
    })
    .then((res: any) => {
      reqMainList.length = 0;
      reqMainList.push(...res.data);

      rLoading.value = false;
    })
    .catch((err: any) => {
      rLoading.value = false;
      proxy.$alert(err, '提示');
    });
}
</script>

<style lang="scss" scoped>
#financeSettleByDiv {
  width: 100%;
  height: 100%;

  .splitpanes {
    height: calc(100% - 40px);
  }

  .el-tabs {
    height: 100%;
    :deep(.el-tabs__content) {
      height: calc(100% - 40px);
      padding: 0;
      .el-tab-pane {
        height: 100%;
      }
    }
  }
  // 右侧保存按钮
  .saveBtn {
    //float: right;
    margin: 0px 30px;
  }
  // 附加按钮间距
  .button-blue {
    margin: 5px 0 5px 5px;
  }
  .button-red {
    margin: 5px 0 5px 5px;
  }

  // 机构表格
  :deep(.vxe-table--render-default) {
    height: 100%;
  }
  :deep(.vxe-table--render-wrapper) {
    height: 100%;
    background-color: $base-white;
  }
  .tableBox {
    height: calc(100%);
  }
  .tableBoBox {
    height: calc(100%);
  }

  .tableMoSaBox {
    height: calc(100% - 40px);
  }

  .searchform {
    padding: 0 0 0 10px;

    :deep(.vxe-form--item) {
      margin-right: 30px;
    }
  }

  .searchform .vxe-input {
    --el-input-width: 120px;
  }

  .searchform .vxe-select {
    --el-select-width: 120px;
  }
  .form-input-m {
    .el-input {
      width: 150px;
    }

    .vxe-input {
      width: 150px !important;
    }
  }
}
</style>